package longrise.phone.com.bjjt_henan.Adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.bjjt.util.UiUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;

/**
 * Created by Administrator on 2016/5/14.
 */
public class AccidentLoadPicAdapter extends BaseAdapter {

    private Context mContext;

    private List<EntityBean> lists;

    private String isSingle;

    public AccidentLoadPicAdapter(Context context) {
        this.mContext = context;
    }

    public void setIsSingle(String isSingle) {
        this.isSingle = isSingle;
    }

    public void setLists(List<EntityBean> lists) {
        this.lists = lists;
    }

    public void addData(EntityBean bean) {
        if (lists == null) {
            lists = new ArrayList<EntityBean>();
        }
        lists.add(bean);
    }

    @Override
    public int getCount() {
        return (null == lists) ? 0 : lists.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_accidentloadpic_item, null);
            holder.picIV = (ImageView) convertView.findViewById(R.id.alpic_iv);
            holder.infoTV = (TextView) convertView.findViewById(R.id.alpic_tv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        EntityBean bean = lists.get(position);
        showItem(holder, bean, position);

        return convertView;
    }

    private void showItem(ViewHolder holder, EntityBean bean, int pos) {
        String type = bean.getString("type");
        int imgId = -1;
        switch (Integer.valueOf(type)) {
            case 0:
                if (TextUtils.equals(isSingle, "0")) {
                    imgId = R.mipmap.upload_icon01;
                    holder.infoTV.setText("车辆侧前方");
                } else {
                    imgId = R.mipmap.tra_img01;
                    holder.infoTV.setText("车头前方照片");
                }
                break;
            case 1:
                if (TextUtils.equals(isSingle, "0")) {
                    imgId = R.mipmap.upload_icon02;
                    holder.infoTV.setText("车辆侧后方");
                } else {
                    imgId = R.mipmap.tra_img02;
                    holder.infoTV.setText("车尾后方照片");
                }
                break;
            case 2:
                if (TextUtils.equals(isSingle, "0")) {
                    imgId = R.mipmap.upload_icon03;
                    holder.infoTV.setText("车辆受损部位");
                } else {
                    imgId = R.mipmap.tra_img03;
                    holder.infoTV.setText("车辆受损部位照片");
                }
                break;
            case 3:
                imgId = R.mipmap.tra_img04;
                holder.infoTV.setText("事故侧方全景照片");
                break;
            case 4:
                imgId = R.mipmap.tra_img05;
                holder.infoTV.setText("对方驾驶证照片");
                break;
            case 5:
                imgId = R.mipmap.tra_img06;
                holder.infoTV.setText("对方行驶证照片");
                break;
            case 20:
                imgId = R.mipmap.btra_img01_un;
                holder.infoTV.setText("其他定责照片");
                break;
            case -1:
                imgId = R.mipmap.add02;
                holder.infoTV.setText("继续添加");
                break;
            case -2:
                imgId = R.mipmap.add02;
                holder.infoTV.setText("受损部位2");
                break;
            case -3:
                imgId = R.mipmap.add02;
                holder.infoTV.setText("受损部位3");
                break;
            case 11://对方受损部位
                imgId = R.mipmap.btra_img01;
                holder.infoTV.setText("受损部位" + (pos + 1));
                break;
            case 12://己方受损部位
                imgId = R.mipmap.btra_img01;
                holder.infoTV.setText("受损部位" + (pos + 1));
                break;
            default:
                imgId = R.mipmap.add02;
                holder.infoTV.setText("受损部位" + (pos + 1));
                break;
        }
        //默认图片路径
        String imgPath = "drawable://" + imgId;
        //网络路径
        String uri = bean.getString("url");
        if (TextUtils.isEmpty(uri)) {
            //本地路径
            String path = bean.getString("path");
            if (TextUtils.isEmpty(path)) {
                uri = imgPath;
            } else {
                uri = "file://" + path;
            }
        }
        ImageLoader.getInstance().displayImage(uri, holder.picIV, CommontUtils.getOptions());
    }

    //更新指定Item
    public void updateItem(int refreshItemPostion, final View view) {
        if (refreshItemPostion > lists.size()) {
            UiUtil.showToast(mContext, "error：超出列表范围");
            return;
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        showItem(holder, lists.get(refreshItemPostion), refreshItemPostion);
    }

    /**
     * 局部更新数据，调用一次getView()方法；Google推荐的做法
     *
     * @param listView 要更新的listview
     * @param position 要更新的位置
     */
    public void notifyDataSetChanged(ListView listView, int position) {
        /**第一个可见的位置**/
        int firstVisiblePosition = listView.getFirstVisiblePosition();
        /**最后一个可见的位置**/
        int lastVisiblePosition = listView.getLastVisiblePosition();

        /**在看见范围内才更新，不可见的滑动后自动会调用getView方法更新**/
        if (position >= firstVisiblePosition && position <= lastVisiblePosition) {
            /**获取指定位置view对象**/
            View view = listView.getChildAt(position - firstVisiblePosition);
            getView(position, view, listView);
        }
    }

    private class ViewHolder {
        private ImageView picIV;
        private TextView infoTV;
    }
}
